package com.pj.wisdomlogistics.ui.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.QuLeaderInfo;
import com.pj.wisdomlogistics.mvp.contract.QuLeaderContract;
import com.pj.wisdomlogistics.mvp.frame.MvpActivity;
import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.mvp.presenter.QuLeaderPresenter;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.adapter.QuLeaderAdapter;
import com.pj.wisdomlogistics.ui.view.recyclerview.BaseViewHolder;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class QuLeaderActivity extends MvpActivity<QuLeaderPresenter> implements QuLeaderContract.View, QuLeaderAdapter.listener {
    @BindView(R.id.rv_leader)
    RecyclerView mRvLeader;

    private QuLeaderAdapter mAdapter;

    private String leaderId;
    private String leaderName;
    private int mSelectedPos = -1;
    private List<QuLeaderInfo> mList;

    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @Override
    public void initView() {
        mRvLeader.setLayoutManager(new GridLayoutManager(this , 2 , LinearLayoutManager.VERTICAL , false));
        mAdapter = new QuLeaderAdapter(this);
        mRvLeader.setAdapter(mAdapter);

        mAdapter.setListener(this);

        mPresenter.checkLeader();
        showProgressDialog();

    }



    @Override
    public void initData() {

    }

    @Override
    public int getContentView() {
        return R.layout.activity_qu_leader;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }

    @OnClick({R.id.iv_back , R.id.tv_sure})
    public void onClick(View mView){
        switch (mView.getId()){
            case R.id.iv_back:
                ActivityManagerUtil.create().finishActivity();
                break;
            case R.id.tv_sure:
                EventBus.getDefault().post(new EventCenter<>(Cons.MealInfo.LEADER_ID , leaderId + "," + leaderName));
                ActivityManagerUtil.create().finishActivity();
                break;
        }
    }



    @Override
    public void onError(String errorMsg) {
        dismissProgressDialog();
        Toast.makeText(this, errorMsg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSuccess(List<QuLeaderInfo> mInfos) {
        mList = mInfos;
        dismissProgressDialog();
        mAdapter.setList(mInfos);
    }


    @Override
    public void itemClick(int position , String id , String name) {
        leaderId = id;
        leaderName = name;

        BaseViewHolder vh;
        if(mSelectedPos == -1){
            vh = (BaseViewHolder) mRvLeader.findViewHolderForLayoutPosition(position);
        }else {
            vh = (BaseViewHolder) mRvLeader.findViewHolderForLayoutPosition(mSelectedPos);
        }

        if(vh != null){
            vh.getView(R.id.ll_layout).setBackgroundResource(R.drawable.select);
            vh.getImageView(R.id.iv_icon).setImageResource(R.drawable.people_blue);
        }else{
            mAdapter.notifyItemChanged(mSelectedPos);
        }

        if(mSelectedPos == -1){
            mSelectedPos = position;
            mList.get(mSelectedPos).setSelected(true);
        }else{
            mList.get(mSelectedPos).setSelected(false);
            mSelectedPos = position;
            mList.get(mSelectedPos).setSelected(true);
            vh.getView(R.id.ll_layout).setBackgroundResource(R.drawable.portion_bg_normal);
            vh.getImageView(R.id.iv_icon).setImageResource(R.drawable.people_white);
            vh = (BaseViewHolder) mRvLeader.findViewHolderForLayoutPosition(mSelectedPos);
            vh.getView(R.id.ll_layout).setBackgroundResource(R.drawable.select);
            vh.getImageView(R.id.iv_icon).setImageResource(R.drawable.people_blue);
        }
    }
}
